<?php

/**
 * Appstore每日排名跟踪 
 *
 * @author haifeng.wu
 */

require_once 'CommonController.php';

class Admin_AppstoreController extends Admin_CommonController
{
    /**
     * 初始化函数
     * 
     * @return void
     */
    public function init()
    {
        parent::init();
        $this->view->title = "管理中心";
        $this->_infoModel = new Application_Model_AppstoreInfo();
        $this->_rankModel = new Application_Model_AppstoreRank();
        $this->username = Application_Model_UserLogin::getInstance()->getLoginedUserName();
    }

    /**
     * Appstore列表
     * @author wuhaifeng
     * @return void
     */
    public function listAction(){
        $schoolArray = array(
                            '6004'=>'体育',
                            '6013'=>'健康健美',
                            '6020'=>'医疗',
                            '6006'=>'参考',
                            '6000'=>'商业',
                            '6018'=>'图书',
                            '6001'=>'天气',
                            '6016'=>'娱乐',
                            '6010'=>'导航',
                            '6002'=>'工具',
                            '6021'=>'报刊杂志',
                            '6008'=>'摄影与录像',
                            '6007'=>'效率',
                            '6017'=>'教育',
                            '6009'=>'新闻',
                            '6003'=>'旅行',
                            '6014'=>'游戏',
                            '6012'=>'生活',
                            '6005'=>'社交',
                            '6015'=>'财务',
                            '6011'=>'音乐'
        );
        $feedtypeArray = array('topfreeapplications'=>'免费应用软件排名','topfreeipadapplications'=>'免费ipad应用程序排名');
        $this->view->schoolArray = $schoolArray;
        $this->view->feedtypeArray = $feedtypeArray;
        //获取当前页码
        $pageSize = 20;
        $pageNo = intval($this->_request->get('pageNo'));
        if (empty($pageNo)) {
            $pageNo = 1;
        }
        
        //设置URL模板
        $urls = array();
        $wheres = array();
        
        $appstoreName = trim($this->_request->getPost('appstoreName'));//关键字
        $this->view->appstoreName  = $appstoreName;
        if (!empty($title)) {;
            $wheres[] = " `appstoreName` LIKE '%" . addslashes($appstoreName) . "%'";
        }
        
        $urls['pageNo'] = '{page}';
        $urlTemplate = $this->view->projectUrl($urls);

        //获取总页数以及记录
        $where = ' WHERE 1 ';
        $where .= count($wheres) > 0 ? ' AND ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "SELECT * FROM `{$this->_infoModel->getTableName()}` " . $where . " ORDER BY id DESC";
        $dbPaginator = new Application_Model_DbPaginator($sql, $pageSize, $pageNo);
        $recordCount = $dbPaginator->getRecodCount();
        //获取分页html字符
        Zend_Loader::loadClass('Project_Paginator');
            
        $paginator = new Project_Paginator($recordCount, $pageSize, $pageNo);
        $paginator->urlTemplateContent = $urlTemplate;
        //分配view(分页处理后的记录集以及分页HTML字符)
        $this->view->pageString = $paginator->getPageString();        
        $this->view->rows = $dbPaginator->getRows();
        $urls['pageNo'] = $pageNo;
        $this->view->pageUrl = $this->view->projectUrl($urls);
    }
    
    /**
     * Appstoreran导出excel
     * @author wuhaifeng
     * @return void
     */
    public function excelAction()
    {
        $schoolArray = array(
                            '6004'=>'体育',
                            '6013'=>'健康健美',
                            '6020'=>'医疗',
                            '6006'=>'参考',
                            '6000'=>'商业',
                            '6018'=>'图书',
                            '6001'=>'天气',
                            '6016'=>'娱乐',
                            '6010'=>'导航',
                            '6002'=>'工具',
                            '6021'=>'报刊杂志',
                            '6008'=>'摄影与录像',
                            '6007'=>'效率',
                            '6017'=>'教育',
                            '6009'=>'新闻',
                            '6003'=>'旅行',
                            '6014'=>'游戏',
                            '6012'=>'生活',
                            '6005'=>'社交',
                            '6015'=>'财务',
                            '6011'=>'音乐'
        );
        $he = array();
        for($i='C';$i<='Z';$i++){
           $he[] = $i;
        }
        $sqlInfo = "SELECT * FROM `{$this->_infoModel->getTableName()}` ";
        $appstoreInfoArrTmp = $this->_infoModel->getAdapter()->fetchAll($sqlInfo);
        $appstoreInfoArr = array();
        foreach ($appstoreInfoArrTmp as $item) {
            $appstoreInfoArr[$item['appstoreId']]['appstoreName'] = $item['appstoreName'];
            $appstoreInfoArr[$item['appstoreId']]['school'] = $item['school'];
        }
        $sql = "SELECT max(rank) as maxRank,min(rank) as minRank,appstoreId,updateAmount,downloadAmount,addTime
                FROM `{$this->_rankModel->getTableName()}`
                GROUP BY DATE_FORMAT(`addTime`, '%Y-%m-%d'),appstoreId
                ORDER BY addTime DESC
                ";
        $rows = $this->_rankModel->getAdapter()->fetchAll($sql);
        foreach ($rows as $k =>$v) {
            $rows[$k]['appstoreName'] = isset($appstoreInfoArr[$v['appstoreId']]['appstoreName']) ? $appstoreInfoArr[$v['appstoreId']]['appstoreName'] : '';
            $rows[$k]['school'] = isset($appstoreInfoArr[$v['appstoreId']]['school']) ? $appstoreInfoArr[$v['appstoreId']]['school'] : '';
            $rows[$k]['addTime'] = substr($v['addTime'],0,10);
            if(empty($rows[$k]['appstoreName'])) {
                unset($rows[$k]);
            }
        }
        $appstoreArr = array();
        $dateArr = array();
        foreach ($rows as $key => $val) {
            $rows[$key]['addTime'] = date('Y-m-d', strtotime($rows[$key]['addTime']));
            $appstoreArr[$val['appstoreId']]['appstoreName'] = $val['appstoreName'];
            $appstoreArr[$val['appstoreId']]['school'] = $val['school'];
            $dateArr[$rows[$key]['addTime']] = $rows[$key]['addTime'];
        }
        $res = array();
        foreach ($rows as $key => $val) {
            $res[$val['addTime']][$val['appstoreId']] = $val;
        }
        
        $heiArr = array();
        $y = 0;
        foreach ($appstoreArr as $key => $val) {
            $heiArr[$key]['appstoreName'] = $val['appstoreName'];
            $heiArr[$key]['school'] = isset($schoolArray[$val['school']]) ? $schoolArray[$val['school']] : '';
            $heiArr[$key]['download'] = $he[$y];
            $heiArr[$key]['update'] = $he[$y+1];
            $heiArr[$key]['rank'] = $he[$y+2];
            $y += 3;
        }
        rsort($dateArr);
        $objPHPExcel = new PHPExcel_PHPExcel();
        $objPHPExcel->getProperties()->setCreator("Yek BI System")
                    ->setLastModifiedBy("Yek BI")
                    ->setTitle("排名")
                    ->setSubject("排名")
                    ->setDescription("")
                    ->setKeywords("")
                    ->setCategory("");
        $endArr = end($heiArr);
        $objPHPExcel->getActiveSheet()->mergeCells('A1:B3');
        $objPHPExcel->getActiveSheet()->mergeCells('C1:' . $endArr['rank'] . '3');
        $objPHPExcel->getActiveSheet()->setCellValue('C1', '耶客B2C类产品App Store下载量');
        $objPHPExcel->getActiveSheet()->setCellValue('A4', '星期');
        $objPHPExcel->getActiveSheet()->mergeCells('A4:A6');
        $objPHPExcel->getActiveSheet()->setCellValue('A4', '星期');
        $objPHPExcel->getActiveSheet()->mergeCells('B4:B6');
        $objPHPExcel->getActiveSheet()->setCellValue('B4', '日期');                
        foreach ($heiArr as $key => $val) {
            $objPHPExcel->getActiveSheet()->mergeCells($val['download'].'4:'.$val['rank'].'4');
            $objPHPExcel->getActiveSheet()->setCellValue($val['download'].'4', $val['appstoreName']);
            $objPHPExcel->getActiveSheet()->mergeCells($val['download'].'5:'.$val['rank'].'5');
            $objPHPExcel->getActiveSheet()->setCellValue($val['download'].'5', $val['school']);
            $objPHPExcel->getActiveSheet()->setCellValue($val['download'].'6', '下载量');
            $objPHPExcel->getActiveSheet()->setCellValue($val['update'].'6', '更新量');
            $objPHPExcel->getActiveSheet()->setCellValue($val['rank'].'6', '排名(最大：最小)');
        }
        
        $i = 7;
        $weekar = array('日','一','二','三','四','五','六');
        foreach ($res as $key => $val) {
            $week = date('w', strtotime($key));
            $objPHPExcel->getActiveSheet()->setCellValue('A'.$i, '星期'.$weekar[$week]);
            $objPHPExcel->getActiveSheet()->setCellValue('B'.$i, $key);
            foreach ($val as $k => $v) {
                $objPHPExcel->getActiveSheet()->setCellValue($heiArr[$k]['download'].$i, $v['downloadAmount']);
                $objPHPExcel->getActiveSheet()->setCellValue($heiArr[$k]['update'].$i, $v['updateAmount']);
                $objPHPExcel->getActiveSheet()->setCellValue($heiArr[$k]['rank'].$i, $v['maxRank'].':'.$v['minRank']);
            }
            $i++;
        }
        $fileName = date('Y-m-d', time()).iconv('UTF-8','GBK//IGNORE','导出排名');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $outputFileName = $fileName.".xls";
        //到浏览器  
        header("Content-Type: application/force-download");  
        header("Content-Type: application/octet-stream");  
        header("Content-Type: application/download");  
        header('Content-Disposition:inline;filename="'.$outputFileName.'"');  
        header("Content-Transfer-Encoding: binary");  
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");  
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");  
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");  
        header("Pragma: no-cache");
        $objWriter->save('php://output');
        exit;
        
    }
    /**
     * Appstoreran导出excel
     * @author wuhaifeng
     * @return void
     */
    public function exportAction()
    {
        $objPHPExcel = PHPExcel_IOFactory::load(PUBLIC_PATH."/data/upload/AppStore.xls");
        $objWorksheet = $objPHPExcel->getActiveSheet('B2C');
        $highestRow = $objWorksheet->getHighestRow();
        $time = time();
//        $liea = array(
//                    'id454755312'=>array(10,9,8),
//                    'id450719586'=>array(13,12,11),
//                    'id452703031'=>array(16,15,14),
//                    'id459049146'=>array(19,18,17),
//                    'id451719330'=>array(22,21,20),
//                    'id455820579'=>array(25,24,23),
//                    'id442666468'=>array(28,27,26),
//                    'id412580729'=>array(31,30,29),
//                    'id433864019'=>array(34,33,32),
//                    'id481942451'=>array(37,36,35),
//                    'id408126283'=>array(40,39,38),
//                    'id431040271'=>array(43,42,41),
//                );
        $liea = array(
                    'id450785470'=>array(4,3,2),
                    'id459387975'=>array(7,6,5),
                );
        foreach ($liea as $k => $v) {
            for ($i = 7; $i <= $highestRow; $i++) {
                $data = array();
                $data['appstoreId'] = $k;
                $data['rank'] = str_replace('名以外','+',trim($objWorksheet->getCellByColumnAndRow($v[0], $i)->getValue()));
                $data['updateAmount'] = trim($objWorksheet->getCellByColumnAndRow($v[1], $i)->getValue());
                $data['downloadAmount'] = trim($objWorksheet->getCellByColumnAndRow($v[2], $i)->getValue());
                
                
                $data['addTime'] = date('Y-m-d H:i:s', strtotime(exceltimtetophp($objWorksheet->getCellByColumnAndRow(1, $i)->getValue())));
                $data['addUser'] = 'system';
                $id = $this->_rankModel->insert($data);
                if ($id) {
                    echo $data['appstoreId'].$data['addTime'].'成功'.'<br>';
                }
            }
        }
        
        exit;
        for ($i = 2; $i <= $highestRow; $i++) {
            $n = $i - 1;
            $field = array();
            $field['memberName'] = trim($objWorksheet->getCellByColumnAndRow(0, $i)->getValue());
            $data['sourceId'] = trim($objWorksheet->getCellByColumnAndRow(1, $i)->getValue());
            $data['sourceSubId'] = trim($objWorksheet->getCellByColumnAndRow(2, $i)->getValue());
            $data['sourceName'] = trim($objWorksheet->getCellByColumnAndRow(3, $i)->getValue());
            $data['addTime'] = date('Y-m-d H:i:s',time());
            $data['addUserName'] = $this->username;

            if ($field['memberName'] == '') {
                echo "第{$n}条，导入失败，渠道名称为空。<br/><br/>\r\n\r\n";
            } else if ($data['sourceId'] == '') {
                echo "第{$n}条，导入失败，sourceId空。<br/><br/>\r\n\r\n";
            } else if ($data['sourceSubId'] == '') {
                echo "第{$n}条，导入失败，sourceSubId为空。<br/><br/>\r\n\r\n";
            } else if ($data['sourceName'] == '') {
                echo "第{$n}条，导入失败，sourceName为空。<br/><br/>\r\n\r\n";
            } else {
                $memberKey = md5($field['memberName']);
                if (!isset($memberTemp[$memberKey])) {
                    $memberCode = $memberModel->getCodeByMemberName($field['memberName']);

                    if (!$memberCode) {
                        $member['status'] = 1;
                        $member['name'] = $field['memberName'];
                        $member['addTime'] = date('Y-m-d H:i:s',time());
                        $member['addUserName'] = $this->username;
                        $memberId = $memberModel->insert($member);
                        $memberUpdate['code'] = "10000{$memberId}";
                        $memberModel->update($memberUpdate, "`id` = {$memberId}");
                        $memberCode = $memberUpdate['code'];
                    }
                    $memberTemp[$memberKey] = $memberCode;
                }

                $data['memberCode'] = $memberTemp[$memberKey];
                $data['productCode'] = $currentProductCode;

                if ($this->_sourceModel->isExists($data['productCode'], $data['sourceId'], $data['sourceSubId']) == 0) {
                    $this->_sourceModel->insert($data);
                    $successNum++;
                    echo "第{$n}条，SourceId“{$data['sourceId']}”导入成功。<br/><br/>\r\n\r\n";
                } else {
                    $update = array();
                    $update['sourceName'] = $data['sourceName'];
                    $update['memberCode'] = $data['memberCode'];
                    $this->_sourceModel->update($update , " productCode = '{$data['productCode']}' AND sourceId = '{$data['sourceId']}' AND sourceSubId = '{$data['sourceSubId']}' ");
                    $updateNum++;
                    echo "第{$n}条，SourceId“{$data['sourceId']}”更新成功。<br/><br/>\r\n\r\n";
                }
            }
        }
        echo "共{$highestRow}条记录；成功导入{$successNum}条记录；成功更新{$updateNum}条记录<script>setTimeout(function(){location.href='{$this->view->projectUrl()}';}, 10000);</script>";
        exit;
    
    }
    /**
     * Appstorerand排名列表
     * @author wuhaifeng
     * @return void
     */
    public function rankAction(){
        $appstoreId = trim($this->_request->get('appstoreId'));
        if (empty($appstoreId)) {
             echo $this->view->message('请正确操作', $this->view->projectUrl(array('action' => 'list'))) ;
             exit;
        }
        $rows = $this->_rankModel->filterData(array('appstoreId' => $appstoreId));
        if (empty($rows)) {
             echo $this->view->message('记录不存在', $this->view->projectUrl(array('action' => 'list'))) ;
             exit;
        }
        
        //获取当前页码
        $pageSize = 20;
        $pageNo = intval($this->_request->get('pageNo'));
        if (empty($pageNo)) {
            $pageNo = 1;
        }
        
        //设置URL模板
        $urls = array();
        $wheres = array();
        $urls['appstoreId'] = $appstoreId;
        $urls['pageNo'] = '{page}';
        $urlTemplate = $this->view->projectUrl($urls);
        $vars = array();
        $vars['startTime'] = trim($this->_request->getPost('startTime'));
        $vars['endTime'] = trim($this->_request->getPost('endTime'));
        if (!empty($vars['startTime'])) {
            $wheres[] = " `addTime` >= '{$vars['startTime']}' ";
        }
        if (!empty($vars['endTime'])) {
            $wheres[] = " `addTime` <= '{$vars['endTime']}' ";
        }
        $this->view->vars = $vars;
        //获取总页数以及记录
        $where = " WHERE `appstoreId`='{$appstoreId}' AND `rank` != '' AND `rank` != '-' ";
        $where .= count($wheres) > 0 ? ' AND ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "SELECT id,max(rank) as maxRank,min(rank) as minRank,appstoreId,updateAmount,downloadAmount,addTime,addUser 
                FROM `{$this->_rankModel->getTableName()}` " . $where . "
                GROUP BY DATE_FORMAT(`addTime`, '%Y-%m-%d') 
                ORDER BY addTime DESC
                ";
        $dbPaginator = new Application_Model_DbPaginator($sql, $pageSize, $pageNo);
        $recordCount = $dbPaginator->getRecodCount();
        //获取分页html字符
        Zend_Loader::loadClass('Project_Paginator');
            
        $paginator = new Project_Paginator($recordCount, $pageSize, $pageNo);
        $paginator->urlTemplateContent = $urlTemplate;
        //分配view(分页处理后的记录集以及分页HTML字符)
        $this->view->pageString = $paginator->getPageString();
        $this->view->rows = $dbPaginator->getRows();
        $arrId = array();
        foreach ($dbPaginator->getRows() as $item){
            $arrId[] = $item['id'];
        }
        $this->view->arrId = serialize($arrId);
        $urls['pageNo'] = $pageNo;
        $this->view->pageUrl = $this->view->projectUrl($urls);
        $sqlInfo = "SELECT * FROM `{$this->_infoModel->getTableName()}` ";
        $appstoreInfoArrTmp = $this->_infoModel->getAdapter()->fetchAll($sqlInfo);
        $appstoreInfoArr = array();
        foreach ($appstoreInfoArrTmp as $item) {
            $appstoreInfoArr[$item['appstoreId']] = $item['appstoreName'];
        }
        $this->view->appstoreInfoArr = $appstoreInfoArr;
    }
    /**
    * 更新rank
    * @author wuhaifeng
    * @return void
    */
    public function updateallAction()
    {
        if ($this->_request->isPost()) {
            $arrId = unserialize($this->_request->getPost('arrId'));
            
            foreach ($arrId as $item) {
                $data['updateAmount'] = trim($this->_request->getPost('update'.$item));
                $data['downloadAmount'] = trim($this->_request->getPost('download'.$item));
                $sql = "SELECT appstoreId,DATE_FORMAT(`addTime`, '%Y-%m-%d') as addtime FROM `{$this->_rankModel->getTableName()}` WHERE `id`='{$item}'";
                $res = $this->_rankModel->getAdapter()->fetchAll($sql);
                $this->_rankModel->update($data, " DATE_FORMAT(`addTime`, '%Y-%m-%d') = '" . $res[0]['addtime'] . "' AND appstoreId = '" . $res[0]['appstoreId'] . "'");
            }
        }
        $backUrl = urldecode($this->_request->get('backUrl'));
        redirect($backUrl);
    }
    /**
    * 插入
    * @author wuhaifeng
    * @return void
    */
    public function insertAction()
    {
        $schoolArray = array(
                            '6004'=>'体育',
                            '6013'=>'健康健美',
                            '6020'=>'医疗',
                            '6006'=>'参考',
                            '6000'=>'商业',
                            '6018'=>'图书',
                            '6001'=>'天气',
                            '6016'=>'娱乐',
                            '6010'=>'导航',
                            '6002'=>'工具',
                            '6021'=>'报刊杂志',
                            '6008'=>'摄影与录像',
                            '6007'=>'效率',
                            '6017'=>'教育',
                            '6009'=>'新闻',
                            '6003'=>'旅行',
                            '6014'=>'游戏',
                            '6012'=>'生活',
                            '6005'=>'社交',
                            '6015'=>'财务',
                            '6011'=>'音乐'
        );
        $feedtypeArray = array('topfreeapplications'=>'免费应用软件排名','topfreeipadapplications'=>'免费ipad应用程序排名');
        if ($this->_request->isPost()) {
            $data['feedType'] = trim($this->_request->getPost('feedType'));
            $data['school'] = trim($this->_request->getPost('school'));
            $data['appstoreId'] = trim($this->_request->getPost('appstoreId'));
            $data['appstoreName'] = trim($this->_request->getPost('appstoreName'));
            $filtdata = $this->_infoModel->filterData(array('appstoreId' => $data['appstoreId']));
            if (!empty($filtdata)) {
                echo $this->view->message($data['appstoreId'].'已存在') ;
                exit;
            }
            if (empty($data['feedType']) || empty($data['school']) || empty($data['appstoreId']) || empty($data['appstoreName'])) {
                echo $this->view->message('请正确填写！') ;
                exit;
            }
            $data['addTime'] = date('Y-m-d H:i:s');
            $data['addUser'] = $this->username;
            $data['url'] = 'http://itunes.apple.com/cn/rss/' . $data['feedType'] . '/limit=300/genre=' . $data['school'] . '/xml';
            $this->_infoModel->insert($data);
            echo $this->view->message('操作成功！', $this->view->projectUrl(array('action' => 'list'))) ;
            exit;
        }
        $this->view->schoolArray = $schoolArray;
        $this->view->feedtypeArray = $feedtypeArray;
    }
    /**
    * 更新
    * @author wuhaifeng
    * @return void
    */
    public function updateAction()
    {
        $id = intval($this->_request->get('id'));
        if (empty($id)) {
             echo $this->view->message('请正确操作', $this->view->projectUrl(array('action' => 'list'))) ;
             exit;
        }
        $rows = $this->_infoModel->filterData(array('id' => $id));
        if (empty($rows)) {
             echo $this->view->message('记录不存在', $this->view->projectUrl(array('action' => 'list'))) ;
             exit;
        }
        if ($this->_request->isPost()) {
            $data['feedType'] = trim($this->_request->getPost('feedType'));
            $data['school'] = trim($this->_request->getPost('school'));
            $data['appstoreId'] = trim($this->_request->getPost('appstoreId'));
            $data['appstoreName'] = trim($this->_request->getPost('appstoreName'));
            if (empty($data['feedType']) || empty($data['school']) || empty($data['appstoreId']) || empty($data['appstoreName'])) {
                echo $this->view->message('请正确填写！') ;
                exit;
            }
            $data['url'] = 'http://itunes.apple.com/cn/rss/' . $data['feedType'] . '/limit=300/genre=' . $data['school'] . '/xml';
            $this->_infoModel->update($data, " `id` = '{$id}' ");
            echo $this->view->message('操作成功！', $this->view->projectUrl(array('action' => 'list'))) ;
            exit;
        }
        $schoolArray = array(
                            '6004'=>'体育',
                            '6013'=>'健康健美',
                            '6020'=>'医疗',
                            '6006'=>'参考',
                            '6000'=>'商业',
                            '6018'=>'图书',
                            '6001'=>'天气',
                            '6016'=>'娱乐',
                            '6010'=>'导航',
                            '6002'=>'工具',
                            '6021'=>'报刊杂志',
                            '6008'=>'摄影与录像',
                            '6007'=>'效率',
                            '6017'=>'教育',
                            '6009'=>'新闻',
                            '6003'=>'旅行',
                            '6014'=>'游戏',
                            '6012'=>'生活',
                            '6005'=>'社交',
                            '6015'=>'财务',
                            '6011'=>'音乐'
        );
        $feedtypeArray = array('topfreeapplications'=>'免费应用软件排名','topfreeipadapplications'=>'免费ipad应用程序排名');
        $this->view->schoolArray = $schoolArray;
        $this->view->feedtypeArray = $feedtypeArray;
        $this->view->rows = $rows;
    }
    
    /**
     * 删除动作
     * 
     * @return void
     */
    public function deleteAction()
    {
        if ($this->_request->isPost()) {
            $ids= $this->_request->getPost('selectId');
            if (is_array($ids)) {
                $this->_infoModel->deleteById($ids);
            }
        }
        $backUrl = urldecode($this->_request->get('backUrl'));
        redirect($backUrl);
    }
    /**
     * 查看每小时的记录
     * 
     * @return void
     */
    public function viewallAction()
    {
        $backUrl = urldecode($this->_request->get('backUrl'));
        $id = intval($this->_request->get('id'));
        $sqlInfo = "SELECT * FROM `{$this->_infoModel->getTableName()}` ";
        $appstoreInfoArrTmp = $this->_infoModel->getAdapter()->fetchAll($sqlInfo);
        $appstoreInfoArr = array();
        foreach ($appstoreInfoArrTmp as $item) {
            $appstoreInfoArr[$item['appstoreId']] = $item['appstoreName'];
        }
        $sqlRank = "SELECT DATE_FORMAT(`addTime`, '%Y-%m-%d') as addTime,appstoreId 
                FROM `{$this->_rankModel->getTableName()}` WHERE `id`='{$id}'
                ";
        $appstoreRankArrTmp = $this->_rankModel->getAdapter()->fetchAll($sqlRank);
        $sql = "SELECT * 
                FROM `{$this->_rankModel->getTableName()}` 
                WHERE DATE_FORMAT(`addTime`, '%Y-%m-%d') = '{$appstoreRankArrTmp[0]['addTime']}' 
                AND appstoreId = '{$appstoreRankArrTmp[0]['appstoreId']}'
                ORDER BY addTime DESC";
        $rows = $this->_rankModel->getAdapter()->fetchAll($sql);
        foreach ($rows as $k=>$v) {
            $rows[$k]['appstoreName'] = isset($appstoreInfoArr[$v['appstoreId']]) ? $appstoreInfoArr[$v['appstoreId']] : $v['appstoreId'];
        }
        $this->view->rows = $rows;
        $this->view->backUrl = $backUrl;
    }
    

    
    
}